/**
 * La classe VistaFranges representa la pantalla en la que l'usuari visualitza
 * i escolleix noves franges horaries.
 * 
 * @author  Felip Moll 41743858P
 * @version 1.0, 6 Juny 2008 
 * 
 */
package vistes;

import java.awt.event.ActionListener;
import javax.swing.table.DefaultTableModel;

public class VistaFranges extends javax.swing.JPanel {

    private String[][] llistaFranges;
    private String[] headFranges = {"HInici", "MInici", "HFi", "MFi", "Taxa"};
    private int numFranges = 0;

    public VistaFranges() {
        initComponents();
        clearLlistaFranges();
    }

    public void setLlistaFranges() {
        ((DefaultTableModel) (lsFranges.getModel())).setDataVector(llistaFranges, headFranges);
    }

    public void setLlistaFranges(String[][] inDatos) {
        llistaFranges = inDatos;
        ((DefaultTableModel) (lsFranges.getModel())).setDataVector(llistaFranges, headFranges);
    }

    public void setSpinners() {
        spFranjaInici.setValue(0);
        spMinIni.setValue(0);
        spFranjaFi.setValue(23);
        spMinFi.setValue(59);

        spFranjaInici.setEnabled(false);
        spMinIni.setEnabled(false);
    }

    public void clearLlistaFranges() {
        llistaFranges = new String[24][4];

        lsFranges.setModel(new javax.swing.table.DefaultTableModel(llistaFranges, headFranges));
        numFranges = 0;
        setSpinners();
    }

    /**
     * Aquesta funcio agafa les dades introduides i les passa en forma d'string
     * @return Un string[A][B], t.q: A = Num de franges introduides, B = 5
     * i [A][0] = Hora inici
     *   [A][1] = Minut inici
     *   [A][2] = Hora Fi
     *   [A][3] = Minut fi
     *   [A][4] = Taxa a aplicar a la franja
     */
    public String[][] getListaFranges() {
        String resultat[][];
        int nfranges = 0;
        while (nfranges < 24 && nfranges < llistaFranges.length && llistaFranges[nfranges][0] != null) {
            nfranges++;
        }
        if (nfranges != 0) {
            //Mirem que la ultima franja arribi a les 23:59, sino li feim arribar
            int hLastF = Integer.parseInt(llistaFranges[nfranges - 1][2]);
            int mLastF = Integer.parseInt(llistaFranges[nfranges - 1][3]);

            if (hLastF == 23) {
                if (mLastF != 59) {
                    llistaFranges[nfranges][0] = "23";
                    llistaFranges[nfranges][1] = "" + (mLastF + 1);
                    llistaFranges[nfranges][2] = "23";
                    llistaFranges[nfranges][3] = "59";
                    llistaFranges[nfranges][4] = "0";
                    nfranges++;
                }
            } else {
                if (mLastF == 59) {
                    hLastF++;
                    mLastF = 0;
                } else {
                    mLastF++;
                }

                llistaFranges[nfranges][0] = "" + hLastF;
                llistaFranges[nfranges][1] = "" + mLastF;
                llistaFranges[nfranges][2] = "23";
                llistaFranges[nfranges][3] = "59";
                llistaFranges[nfranges][4] = "0";
                nfranges++;
            }


            resultat = new String[nfranges][5];

            for (int i = 0; i < nfranges; i++) {
                resultat[i][0] = llistaFranges[i][0];
                resultat[i][1] = llistaFranges[i][1];
                resultat[i][2] = llistaFranges[i][2];
                resultat[i][3] = llistaFranges[i][3];
                resultat[i][4] = llistaFranges[i][4];
            }
            numFranges = nfranges;
            setLlistaFranges(resultat);
            return resultat;
        }
        return null;
    }

    void setActions(ActionListener actionListener) {
        bGuarda.addActionListener(actionListener);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        spFranjaInici = new javax.swing.JSpinner();
        spFranjaFi = new javax.swing.JSpinner();
        spTaxa = new javax.swing.JSpinner();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lsFranges = new javax.swing.JTable();
        bGuarda = new javax.swing.JButton();
        bAddFranja = new javax.swing.JButton();
        jLabel4 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        spMinIni = new javax.swing.JSpinner();
        spMinFi = new javax.swing.JSpinner();
        jLabel6 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();

        spFranjaInici.setModel(new javax.swing.SpinnerNumberModel(0, 0, 23, 1));
        spFranjaInici.setEnabled(false);

        spFranjaFi.setModel(new javax.swing.SpinnerNumberModel(23, 0, 23, 1));

        spTaxa.setModel(new javax.swing.SpinnerNumberModel(Double.valueOf(0.0d), Double.valueOf(0.0d), null, Double.valueOf(1.0d)));

        jLabel1.setText("Hora d'inici:");

        jLabel2.setText("Hora de fi:   Minut fi:");

        jLabel3.setText("Taxa:");

        lsFranges.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "HInici", "MInici", "HFi", "MFi", "Taxa"
            }
        ));
        jScrollPane1.setViewportView(lsFranges);

        bGuarda.setText("Guardar al Repositori");

        bAddFranja.setText("Afegir franja");
        bAddFranja.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                bAddFranjaActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("DejaVu Sans", 1, 13));
        jLabel4.setText("Parametres de la franja:");

        spMinIni.setModel(new javax.swing.SpinnerNumberModel(0, 0, 59, 1));
        spMinIni.setEnabled(false);

        spMinFi.setModel(new javax.swing.SpinnerNumberModel(59, 0, 59, 1));

        jLabel6.setText("Minut inici:");

        jButton1.setText("Neteja");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(236, 236, 236)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jButton1)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 311, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(49, 49, 49)
                        .addComponent(bGuarda, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(47, 47, 47))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3, javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                    .addComponent(spTaxa)
                                    .addComponent(spFranjaInici)
                                    .addComponent(jLabel1))
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(20, 20, 20)
                                        .addComponent(bAddFranja, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                        .addGap(6, 6, 6)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(spMinIni, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(jLabel6))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 37, Short.MAX_VALUE)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel2)
                                            .addGroup(layout.createSequentialGroup()
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(spFranjaFi, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(spMinFi, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)))))))
                        .addContainerGap(256, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGap(306, 306, 306)
                .addComponent(jLabel4)
                .addContainerGap(323, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(jLabel4)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(spFranjaInici, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spMinIni, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spMinFi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(spFranjaFi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel3)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(bAddFranja)
                    .addComponent(spTaxa, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButton1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 274, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(56, 56, 56))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(543, Short.MAX_VALUE)
                .addComponent(bGuarda)
                .addGap(32, 32, 32))
        );
    }// </editor-fold>//GEN-END:initComponents
    private void bAddFranjaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_bAddFranjaActionPerformed

        //Nombre de franges que tenim actualment al vector de franges
        // numFranges = llistaFranges.length;
        if (numFranges == 0) {
            llistaFranges = new String[24][5];
        }

        boolean correcte = true;
        //Hores que ens acaben d'arribar
        int hInici = (Integer) (spFranjaInici.getValue());
        int minInici = (Integer) (spMinIni.getValue());
        int hFi = (Integer) (spFranjaFi.getValue());
        int minFi = (Integer) (spMinFi.getValue());

        //Taxa que ens acaba d'arribar i taxa de la ultima franja
        String taxa = "" + spTaxa.getValue();
        String lastTaxa;

        if (numFranges > 0) {
            lastTaxa = llistaFranges[numFranges - 1][4];
        } else {
            lastTaxa = "0";
        }



        //Mirem la coherencia individual d'aquesta franja
        if (hInici == hFi) {
            if (minInici >= minFi) {
                correcte = false;
            }
        } else if (hInici > hFi) {
            correcte = false;
        }

        //Si es correcte mirem la coherencia amb la ultima franja
        //Si es que n'hi ha
        if (correcte) {
            if (numFranges != 0) {
                int hLastF = Integer.parseInt(llistaFranges[numFranges - 1][2]);
                int mLastF = Integer.parseInt(llistaFranges[numFranges - 1][3]);
                if (hInici == hLastF) {
                    if (minInici <= mLastF) {
                        correcte = false;
                    }
                } else if (hInici < hLastF) {
                    correcte = false;
                }
            }
        }


        //Sabem que es coherent amb l'ultima franja, per tant nomes cal mirar
        //si s'ha de afegir a la franja o s'ha de crear una de nova
        if (correcte) {
            if (taxa.equals(lastTaxa) && numFranges != 0) {
                llistaFranges[numFranges - 1][2] = String.valueOf(hFi);
                llistaFranges[numFranges - 1][3] = String.valueOf(minFi);
                //Envia la llista a les vistes!
                setLlistaFranges(llistaFranges);
            } else {
                //Afegim nova franja a la matriu
                llistaFranges[numFranges][0] = String.valueOf(hInici);
                llistaFranges[numFranges][1] = String.valueOf(minInici);
                llistaFranges[numFranges][2] = String.valueOf(hFi);
                llistaFranges[numFranges][3] = String.valueOf(minFi);
                llistaFranges[numFranges][4] = String.valueOf(taxa);
                numFranges++;
                setLlistaFranges(llistaFranges);
            }

            //Set Spinners
            if (minFi == 59) {
                minFi = 0;
                if (hFi == 23) {
                    hFi = 0;
                } else {
                    hFi++;
                }
                spFranjaInici.setValue(hFi);
                spMinIni.setValue(minFi);
            } else {
                spFranjaInici.setValue(hFi);
                spMinIni.setValue(minFi + 1);
            }
        }
}//GEN-LAST:event_bAddFranjaActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        clearLlistaFranges();
        spFranjaInici.setValue(0);
        spMinIni.setValue(0);
        spFranjaFi.setValue(23);
        spMinFi.setValue(59);
        spTaxa.setValue(0);
    }//GEN-LAST:event_jButton1ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton bAddFranja;
    private javax.swing.JButton bGuarda;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTable lsFranges;
    private javax.swing.JSpinner spFranjaFi;
    private javax.swing.JSpinner spFranjaInici;
    private javax.swing.JSpinner spMinFi;
    private javax.swing.JSpinner spMinIni;
    private javax.swing.JSpinner spTaxa;
    // End of variables declaration//GEN-END:variables
}
